package com.ftar.urgent.camera;


import android.content.Context;
import android.graphics.Point;
import android.hardware.Camera;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraMetadata;
import android.util.Size;
import android.view.TextureView;

import com.ftar.camera.Camera2Helper;
import com.ftar.camera.listener.Camera2Listener;
import com.ftar.urgent.constant.CameraConfig;

/**
 * @author: wei.jin
 * @date: 2023/7/3
 * @email: wei.jin@fanttec.com
 * @description：
 */
public class Camera2Manager extends BaseCameraManager {
    private String TAG = this.getClass().getSimpleName();
    private Context mContext;
    private Camera2Listener mOnCameraListener;
    private Camera2Helper mCamera;

    public Camera2Manager(Context context) {
        mContext = context;
    }

    @Override
    public void openCamera(TextureView cameraView) {
        super.openCamera(cameraView);

        mCamera = new Camera2Helper.Builder()
                .previewOn(cameraView)
                .context(mContext)
                .specificCameraId(Camera2Helper.CAMERA_ID_BACK)
                .previewSize(new Point(CameraConfig.CAMERA_PREVIEW_WIDTH, CameraConfig.CAMERA_PREVIEW_HEIGHT))
                .cameraListener(camera2Listener)
                .maxFps(CameraConfig.CAMERA_FPS)
                .focusMode(CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_VIDEO) //CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE CONTROL_AF_MODE_OFF
                .focusDistance(4f) //5.8f
                .zoomInLevel(2.6f)
                .build();

        mCamera.start();
    }


    @Override
    public Camera2Helper getCameraObject() {
        return mCamera;
    }

    @Override
    public void setDistance(float distance) {
        mCamera.setDistance(distance);
    }

    @Override
    public void zoomIn(float level) {
        mCamera.zoomIn(level);
    }

    @Override
    public void zoomOut() {
        mCamera.zoomOut();
    }

    private Camera2Listener camera2Listener = new Camera2Listener() {

        @Override
        public void onCameraOpened(CameraDevice camera, String cameraId, Size previewSize, int displayOrientation, boolean isMirror) {
            mOnCameraListener.onCameraOpened(camera, cameraId, previewSize, displayOrientation, isMirror);
        }

        @Override
        public void onPreview(byte[] data, Camera camera) {
            mOnCameraListener.onPreview(data, null);

        }

        @Override
        public void onCameraClosed() {

        }

        @Override
        public void onCameraError(Exception e) {

        }

        @Override
        public void onCameraConfigurationChanged(int cameraID, int displayOrientation) {

        }
    };


    @Override
    public void setOnCameraPreViewListener(Camera2Listener cameraListener) {
        mOnCameraListener = cameraListener;
    }

    @Override
    public void closeCamera() {
    }
}
